/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.addressBook.baseService;

import com.addressBook.bean.UserInfo;
import com.addressBook.util.AllUtil;
import java.util.ArrayList;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;



/**
 *
 * @author Lam
 */
public class Search {

    public ArrayList<UserInfo> baseSearch(String regex) {
        ArrayList<UserInfo> newList = new ArrayList<UserInfo>();
        for (UserInfo bean : AllUtil.beanArrayList) {
            if (beanSearch(bean, regex)) {
                newList.add(bean);
            }
        }
        return newList;
    }

    public boolean beanSearch(UserInfo bean, String regex) {
        Hanyu hy = new Hanyu();
        String regexStr=hy.getStringPinYin(regex);
        if (!bean.getN().isEmpty()) {
            String name = hy.getStringPinYin(bean.getN());
            if (name.contains(regexStr)) {
                return true;
            }
        }
        if (!bean.getFN().isEmpty()) {
            String Fname = hy.getStringPinYin(bean.getFN());
            if (Fname.contains(regexStr)) {
                return true;
            }
        }
        if (!bean.getTEL().isEmpty()) {
            for (String tel : bean.getTEL()) {
                if (tel.contains(regex)) {
                    return true;
                }
            }
        }
        return false;
    }

    private class Hanyu {

        private HanyuPinyinOutputFormat format = null;
        private String[] pinyin;

        public Hanyu() {
            format = new HanyuPinyinOutputFormat();
            format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

            pinyin = null;
        }

        //转换单个字符  
        public String getCharacterPinYin(char c) {
            try {
                pinyin = PinyinHelper.toHanyuPinyinStringArray(c, format);
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }

            // 如果c不是汉字，toHanyuPinyinStringArray会返回null  
            if (pinyin == null) {
                return null;
            }

            // 只取一个发音，如果是多音字，仅取第一个发音  
            return pinyin[0];
        }

        //转换一个字符串  
        public String getStringPinYin(String str) {
            StringBuilder sb = new StringBuilder();
            String tempPinyin = null;
            for (int i = 0; i < str.length(); ++i) {
                tempPinyin = getCharacterPinYin(str.charAt(i));
                if (tempPinyin == null) {
                    // 如果str.charAt(i)非汉字，则保持原样  
                    sb.append(str.charAt(i));
                } else {
                    sb.append(tempPinyin);
                }
            }
            return sb.toString();
        }
    }

}
